package com.whlilong.jxc.dialog;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.whlilong.jxc.model.Goodsbrand;
import com.whlilong.jxc.model.Goodsinfo;
import com.whlilong.jxc.model.Hsinfo;
import com.whlilong.jxc.model.Keshang;
import com.whlilong.jxc.model.Kstype;
import com.whlilong.jxc.sys.CnToSpellFirstChar;
import com.whlilong.jxc.sys.ExtentionInfo;
import com.swtdesigner.ResourceManager;

public class GoodsInfoCreate extends AbstractDialog {

	private List<Goodsinfo> list = ExtentionInfo.getInstance()
			.getGoodsInfoList();
	private Text goodsIdText;
	private Text goodsNameText;
	private Text pymText;
	private Text ckjjText;
	private Text pfjText;
	private Text lsjText;
	private Text unitText;
	private Text ggText;
	private Text hyjText;
	private Text text_15;
	private Text text_16;
	private Text text_17;
	private Text text_18;
	private Text text_19;
	private Text goodsNoText;
	private TableViewer tableViewer;
	private Combo goodsDistrictCombo;
	private Combo goodsTypeCombo;
	private Combo goodsBrandCombo;
	private Combo businessCombo;

	public GoodsInfoCreate(Shell parentShell, TableViewer tableViewer) {
		super(parentShell);
		this.tableViewer = tableViewer;
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("新建数据");
	}

	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, Add_id, "继续添加(&A)", false);
		createButton(parent, IDialogConstants.OK_ID, "添加(&S)", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "取消(&X)", false);

	}

	public Control createGroupText(Composite parent) {
		FormLayout formLayout = (FormLayout) parent.getLayout();
		formLayout.marginBottom = 10;
		formLayout.marginRight = 10;
		formLayout.marginLeft = 10;

		CTabFolder tabFolder = new CTabFolder(parent, SWT.BORDER);
		tabFolder.setLayoutData(new FormData());
		tabFolder.setSimple(false);
		tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

		CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE);
		tabItem.setImage(ResourceManager.getPluginImage("com.whlilong.jxc",
				"icons/brand1.gif"));
		tabItem.setText("基本信息");

		Composite composite = new Composite(tabFolder, SWT.BORDER);
		tabItem.setControl(composite);
		GridLayout gl_composite = new GridLayout(6, false);
		gl_composite.verticalSpacing = 10;
		composite.setLayout(gl_composite);

		Label goodsIdLabel = new Label(composite, SWT.NONE);
		goodsIdLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		goodsIdLabel.setAlignment(SWT.RIGHT);
		goodsIdLabel.setText("商品编号：");

		goodsIdText = new Text(composite, SWT.BORDER);
		GridData gd_goodsIdText = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_goodsIdText.widthHint = 90;
		goodsIdText.setLayoutData(gd_goodsIdText);
		goodsIdText.setBounds(0, 0, 73, 23);

		Label goodsNameLabel = new Label(composite, SWT.NONE);
		goodsNameLabel.setAlignment(SWT.RIGHT);
		goodsNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		goodsNameLabel.setBounds(0, 0, 61, 17);
		goodsNameLabel.setText("商品名称：");

		goodsNameText = new Text(composite, SWT.BORDER);
		goodsNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				CnToSpellFirstChar c = new CnToSpellFirstChar();
				String pymStr = c.makePy(goodsNameText.getText());
				pymText.setText(pymStr.substring(0, pymStr.length() - 1)
						.toUpperCase());
				pymText.selectAll();
			}
		});

		GridData gd_goodsNameText = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_goodsNameText.widthHint = 90;
		goodsNameText.setLayoutData(gd_goodsNameText);
		goodsNameText.setBounds(0, 0, 73, 23);

		Label pymLabel = new Label(composite, SWT.NONE);
		pymLabel.setAlignment(SWT.RIGHT);
		pymLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		pymLabel.setBounds(0, 0, 61, 17);
		pymLabel.setText("拼音码：");

		pymText = new Text(composite, SWT.BORDER);
		pymText.setEditable(false);
		pymText.setEnabled(false);
		GridData gd_pymText = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_pymText.widthHint = 90;
		pymText.setLayoutData(gd_pymText);
		pymText.setSize(239, 83);

		Label goodsNoLabel = new Label(composite, SWT.NONE);
		goodsNoLabel.setAlignment(SWT.RIGHT);
		goodsNoLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		goodsNoLabel.setText("商品货号：");

		goodsNoText = new Text(composite, SWT.BORDER);
		goodsNoText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		Label goodsDistrictLabel = new Label(composite, SWT.NONE);
		goodsDistrictLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		goodsDistrictLabel.setAlignment(SWT.RIGHT);
		goodsDistrictLabel.setBounds(0, 0, 61, 17);
		goodsDistrictLabel.setText("商品区域：");

		goodsDistrictCombo = new Combo(composite, SWT.READ_ONLY);
		goodsDistrictCombo.add("不定");
		List districtList = ExtentionInfo.getInstance().getKeshanginfo();
		for (int i = 0; i < districtList.size(); i++) {
			Keshang ks = (Keshang) districtList.get(i);
			goodsDistrictCombo.add(ks.getKeshangarea());
		}
		goodsDistrictCombo.select(0);
		goodsDistrictCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				true, false, 1, 1));

		Label goodsTypeLabel = new Label(composite, SWT.NONE);
		goodsTypeLabel.setAlignment(SWT.RIGHT);
		goodsTypeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		goodsTypeLabel.setBounds(0, 0, 61, 17);
		goodsTypeLabel.setText("商品类别：");

		goodsTypeCombo = new Combo(composite, SWT.READ_ONLY);
		goodsTypeCombo.add("不定");
		List merchantTypeList = ExtentionInfo.getInstance().getKstypeinfo();
		for (int i = 0; i < merchantTypeList.size(); i++) {
			Kstype ks = (Kstype) merchantTypeList.get(i);
			goodsTypeCombo.add(ks.getKstype());
		}
		goodsTypeCombo.select(0);
		goodsTypeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		Label lsjLabel = new Label(composite, SWT.NONE);
		lsjLabel.setAlignment(SWT.RIGHT);
		lsjLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lsjLabel.setText("零售价：");

		lsjText = new Text(composite, SWT.BORDER);
		lsjText.setText("0");
		GridData gd_lsjText = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_lsjText.widthHint = 90;
		lsjText.setLayoutData(gd_lsjText);
		lsjText.setBounds(0, 0, 73, 23);

		Label goodsBrandLabel = new Label(composite, SWT.NONE);
		goodsBrandLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		goodsBrandLabel.setAlignment(SWT.RIGHT);
		goodsBrandLabel.setBounds(0, 0, 61, 17);
		goodsBrandLabel.setText("商品品牌：");

		goodsBrandCombo = new Combo(composite, SWT.READ_ONLY);
		goodsBrandCombo.add("不定");
		List brandList = ExtentionInfo.getInstance().getBrandInfo();
		for (int i = 0; i < brandList.size(); i++) {
			Goodsbrand ks = (Goodsbrand) brandList.get(i);
			goodsBrandCombo.add(ks.getGoodsbrandname());
		}
		goodsBrandCombo.select(0);
		goodsBrandCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		Label businessLabel = new Label(composite, SWT.NONE);
		businessLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		businessLabel.setAlignment(SWT.RIGHT);
		businessLabel.setBounds(0, 0, 61, 17);
		businessLabel.setText("供应商：");

		businessCombo = new Combo(composite, SWT.READ_ONLY);
		businessCombo.add("不定");
		List businessList = ExtentionInfo.getInstance().getBusinessInfoList();
		for (int i = 0; i < businessList.size(); i++) {
			Hsinfo ks = (Hsinfo) businessList.get(i);
			businessCombo.add(ks.getHsname());
		}
		businessCombo.select(0);
		businessCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		Label ckjjLabel = new Label(composite, SWT.NONE);
		ckjjLabel.setAlignment(SWT.RIGHT);
		ckjjLabel.setText("参考进价：");
		ckjjLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));

		ckjjText = new Text(composite, SWT.BORDER);
		ckjjText.setText("0");
		ckjjText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label pfjLabel = new Label(composite, SWT.NONE);
		pfjLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		pfjLabel.setAlignment(SWT.RIGHT);
		pfjLabel.setText("批发价：");

		pfjText = new Text(composite, SWT.BORDER);
		pfjText.setText("0");
		GridData gd_pfjText = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_pfjText.widthHint = 90;
		pfjText.setLayoutData(gd_pfjText);
		pfjText.setBounds(0, 0, 73, 23);

		Label hyjLabel = new Label(composite, SWT.NONE);
		hyjLabel.setAlignment(SWT.RIGHT);
		hyjLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		hyjLabel.setBounds(0, 0, 61, 17);
		hyjLabel.setText("会员价：");

		hyjText = new Text(composite, SWT.BORDER);
		hyjText.setText("0");
		hyjText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label unitLabel = new Label(composite, SWT.NONE);
		unitLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		unitLabel.setAlignment(SWT.RIGHT);
		unitLabel.setBounds(0, 0, 61, 17);
		unitLabel.setText("单位：");

		unitText = new Text(composite, SWT.BORDER);
		GridData gd_unitText = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_unitText.widthHint = 90;
		unitText.setLayoutData(gd_unitText);
		unitText.setBounds(0, 0, 73, 23);

		Label ggLabel = new Label(composite, SWT.NONE);
		ggLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		ggLabel.setAlignment(SWT.RIGHT);
		ggLabel.setBounds(0, 0, 61, 17);
		ggLabel.setText("规格：");

		ggText = new Text(composite, SWT.BORDER);
		GridData gd_ggText = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_ggText.widthHint = 90;
		ggText.setLayoutData(gd_ggText);
		ggText.setBounds(0, 0, 73, 23);

		CTabItem tabItem_1 = new CTabItem(tabFolder, SWT.NONE);
		tabItem_1.setText("辅助信息");

		Composite composite_1 = new Composite(tabFolder, SWT.BORDER);
		tabItem_1.setControl(composite_1);
		composite_1.setLayout(new GridLayout(2, false));

		Label label_15 = new Label(composite_1, SWT.NONE);
		label_15.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		label_15.setText("New Label");

		text_15 = new Text(composite_1, SWT.BORDER);
		text_15.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label label_16 = new Label(composite_1, SWT.NONE);
		label_16.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		label_16.setText("New Label");

		text_16 = new Text(composite_1, SWT.BORDER);
		text_16.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label label_17 = new Label(composite_1, SWT.NONE);
		label_17.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		label_17.setText("New Label");

		text_17 = new Text(composite_1, SWT.BORDER);
		text_17.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label label_18 = new Label(composite_1, SWT.NONE);
		label_18.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		label_18.setText("New Label");

		text_18 = new Text(composite_1, SWT.BORDER);
		text_18.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label label_19 = new Label(composite_1, SWT.NONE);
		label_19.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		label_19.setText("New Label");

		text_19 = new Text(composite_1, SWT.BORDER);
		text_19.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		return parent;
	}

	protected void continueAdd() {
		if (!goodsIdText.getText().equals("") && goodsIdText.getText() != null
				&& !goodsNameText.getText().equals("")
				&& goodsNameText.getText() != null) {
			for (int i = 0; i < list.size(); i++) {
				if (goodsIdText.getText().equals(list.get(i).getGoodsid())) {
					MessageDialog.openError(getShell(), "错误", "编号重复，添加失败！");
					return;
				}
			}
			exeSearch();
			initial();
			tableViewer.setInput(list);
			tableViewer.getTable().select(list.size() - 1);

		} else {
			MessageDialog.openError(getShell(), "错误", "编号或名称为空，添加失败！");
		}

	}

	private void initial() {
		goodsIdText.setText("");
		goodsNameText.setText("");
		pymText.setText("");
		goodsNoText.setText("");
		goodsDistrictCombo.select(0);
		goodsBrandCombo.select(0);
		goodsTypeCombo.select(0);
		businessCombo.select(0);
		lsjText.setText("0");
		ckjjText.setText("0");
		hyjText.setText("0");
		pfjText.setText("0");
		unitText.setText("");
		ggText.setText("");
	}

	protected void exeSearch() {
		if (!goodsIdText.getText().equals("") && goodsIdText.getText() != null
				&& !goodsNameText.getText().equals("")
				&& goodsNameText.getText() != null) {
			for (int i = 0; i < list.size(); i++) {
				if (goodsIdText.getText().equals(list.get(i).getGoodsid())) {
					MessageDialog.openError(getShell(), "错误", "编号重复，添加失败！");
					isAdd = false;
					return;
				}
			}
			Goodsinfo goods = new Goodsinfo();
			if (list.size() > 0) {
				goods.setId(list.get(list.size() - 1).getId() + 1);
			} else
				goods.setId(1);
			goods.setGoodsid(goodsIdText.getText());
			goods.setGoodsname(goodsNameText.getText());
			goods.setGoodshh(goodsNoText.getText());
			goods.setPym(pymText.getText());
			goods.setAddress(goodsDistrictCombo.getText());
			goods.setGoodsbrandname(goodsBrandCombo.getText());
			goods.setGoodstpname(goodsTypeCombo.getText());
			goods.setHsname(businessCombo.getText());
			goods.setCkjj(BigDecimal
					.valueOf(Double.valueOf(ckjjText.getText())));
			goods.setPfj(BigDecimal.valueOf(Double.valueOf(pfjText.getText())));
			goods.setHyj(BigDecimal.valueOf(Double.valueOf(hyjText.getText())));
			goods.setLsj(BigDecimal.valueOf(Double.valueOf(lsjText.getText())));
			goods.setUnit(unitText.getText());
			goods.setGg(ggText.getText());

			list.add(goods);
			isAdd = true;
		} else {
			MessageDialog.openError(getShell(), "错误", "编号或名称为空，添加失败！");
			isAdd = false;
		}
	}

	protected void okPressed() {
		if (isAdd) {
			setReturnCode(OK);
			close();
		}
	}

	public boolean isAdd() {
		return isAdd;
	}

	public void setAdd(boolean isAdd) {
		this.isAdd = isAdd;
	}

	public List<Goodsinfo> getList() {
		return list;
	}

	public void setList(List<Goodsinfo> list) {
		this.list = list;
	}
}
